layui.define('jquery', function(exports) {
	var $ = layui.jquery;
	var schedule = {
		config:null,
		init: function(options) {
			this.config=options;
			var oDate = new Date(); //定义时间
			oDate.setMonth(oDate.getMonth()); //设置月份
			var year = oDate.getFullYear(); //年
			var month = oDate.getMonth(); //月
			var today = oDate.getDate(); //日
			var e = options.days;//自设定日期
			var ele=options.ele;//容器id
			//计算本月有多少天
			var allDay = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month];
			//判断闰年
			if(month == 1) {
				if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
					allDay = 29;
				}
			}
			//判断本月第一天是星期几
			oDate.setDate(1); //时间调整到本月第一天
			var week = oDate.getDay(); //读取本月第一天是星期几

			//插入空白
			var dom = "";
			for(var i = 0; i < week; i++) {
				dom += "<li></li>";
			}
			var m = month + 1;
			if(m < 10) {
				m = "0" + m;
			}
			var weekdom='<h4 id="test">'+year + "-" + m+"-" + today+'</h4>'+
			'<ul class="week radius"><li>日</li><li>一</li><li>二</li><li>三</li><li>四</li><li>五</li><li>六</li></ul>'+
			'<ul class="dateList"></ul>';
			$(ele).empty().append(weekdom);
									
									
			//日期插入到dateList
			for(var i = 1; i <= allDay; i++) {
				var setFlag = true;
				for(var k = 0; k < e.length; k++) {
					if(i == e[k]) {
						if(i === today) {
							dom += "<li class='today'><span>" + i + "<i class='set-schedule'></i></span></li>";
						} else if(i<today) {
							
							dom += "<li><span class='outdate'>" + i + "<i class='set-schedule'></i></span></li>";
						}else{
							dom += "<li><span>" + i + "<i class='set-schedule'></i></span></li>";
						}
						setFlag = false;
						break;
					}
				}
				if(setFlag) {
					if(i === today) {
						dom += "<li class='today'><span>" + i + "</span></li>";
					}else if(i<today){
						dom += "<li><span class='outdate'>" + i + "</span></li>";
					} else {
						dom += "<li><span>" + i + "</span></li>";

					}

				}
			}
			var dombox=ele+" .dateList";
			$(dombox).empty().append(dom);

		},
		on: function(callback) {
			var elebox=this.config.ele+" li";
		    $(elebox).each(function () {
		        $(this).click(function(){
		        	var val = $(this).text();
		        	$(elebox).removeClass("today");
		        	$(this).addClass("today");
		        	callback(val);
		        })
		    });
		}
	};

	exports('schedule', schedule);
});